/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 *
 */

import fs from 'fs';
import path from 'path';

import { DEFAULT_HVIGOR_CONFIG_JSON_FILE_NAME } from '../const/const.js';
import { HVIGOR_PROJECT_WRAPPER_HOME } from '../const/path-const.js';
import { HvigorConfigJson } from '../type';

import { parseJsonFile } from './parse-json/parse-json-util';

/**
 * 读取用户工程目录下的hvigor-config.json5
 *
 * @returns {HvigorConfigJson} hvigor-config.json5
 */
export function readProjectHvigorConfig(): HvigorConfigJson {
  const hvigorConfigJsonPath = path.resolve(HVIGOR_PROJECT_WRAPPER_HOME, DEFAULT_HVIGOR_CONFIG_JSON_FILE_NAME);

  if (!fs.existsSync(hvigorConfigJsonPath)) {
    return { properties: { 'hvigor.dependency.useNpm': true }, modelVersion: '', dependencies: {} };
  }
  return parseJsonFile(hvigorConfigJsonPath) as HvigorConfigJson;
}
